<?php

class Model_Items {
	
	protected $item_id = null;
	protected $name = '';
	protected $description = '';
	protected $user_id;
	protected $category_id;
	protected $publish_date = '';
	protected $modified_date = '';
	protected $expires = null;
	protected $price = 0;
	protected $currency_code = 'GBP';
	protected $contact_name;
	protected $contact_email;
	protected $premium = 0;
	protected $enabled = 1;
	protected $active = 0;
	protected $spam = 0;
	protected $secret = '';
	protected $show_email = 0;
	protected $slug;
	protected $urgent = 0;
	protected $address_line_1;
	protected $address_line_2;
	protected $local_area;
	protected $location_id;
	protected $postcode;
	protected $use_in_map;
	protected $youtube_url;
	protected $tags = '';
	protected $type = 1;
	protected $views = 0;
	
	public function __construct(array $options = null) {
		if (is_array ( $options )) {
			$this->setOptions ( $options );
		}
	}
	
	function getElapsedTime() {
		if ($this->modified_date == '') {
			return false;
		}
		
		$date = new DateTime ( $this->modified_date );
		
		$diff = date_diff_array ( $date, new DateTime ( 'now' ) );
		$diff ['day'] = ($diff ['week'] * 7) + $diff ['day'];
		unset ( $diff ['week'] );
		
		//print_r ( $diff );
		$elapsed = array ();
		$elapsed ['count'] = 0;
		$elapsed ['unit'] = 'minute';
		
		if ($diff ['year'] > 0) {
			$elapsed ['count'] = $diff ['year'];
			$elapsed ['unit'] = 'year';
		} else {
			if ($diff ['month'] > 0) {
				$elapsed ['count'] = $diff ['month'];
				$elapsed ['unit'] = 'month';
			} else {
				if ($diff ['day'] > 0) {
					$elapsed ['count'] = $diff ['day'];
					$elapsed ['unit'] = 'day';
				} else {
					if ($diff ['hour'] > 0) {
						$elapsed ['count'] = $diff ['hour'];
						$elapsed ['unit'] = 'hour';
					} else {
						if ($diff ['minute'] > 0) {
							$elapsed ['count'] = $diff ['minute'];
							$elapsed ['unit'] = 'minute';
						} else {
							$elapsed ['count'] = 1;
							$elapsed ['unit'] = 'minute';
						}
					}
				
				}
			
			}
		
		}
		
		/*if ($diff ['minute'] > 0) {
			$elapsed ['count'] = $diff ['minute'];
			$elapsed ['unit'] = 'minute';
		} else if ($diff ['minute'] > 30) {
			$elapsed ['hour'] = $elapsed + 1;
		} else {
			$elapsed ['count'] = 1;
			$elapsed ['unit'] = 'minute';
			return $elapsed;
		}
		
		if ($diff ['hour'] > 0) {
			$elapsed ['count'] = $diff ['hour'];
			$elapsed ['unit'] = 'hour';
		} else if ($diff ['hour'] > 12) {
			$elapsed ['day'] = $elapsed ['day'] + 1;
		} else {
			$elapsed ['count'] = 1;
			$elapsed ['unit'] = 'hour';
			return $elapsed;
		}
		
		if ($diff ['day'] > 0) {
			$elapsed ['count'] = $diff ['day'];
			$elapsed ['unit'] = 'day';
		} else if ($diff ['day'] > 15) {
			$elapsed ['month'] = $elapsed ['month'] + 1;
		} else {
			$elapsed ['count'] = 1;
			$elapsed ['unit'] = 'day';
			return $elapsed;
		}
		
		if ($diff ['month'] > 0) {
			$elapsed ['count'] = $diff ['month'];
			$elapsed ['unit'] = 'month';
		} else if ($diff ['month'] > 6) {
			$elapsed ['year'] = $elapsed ['year'] + 1;
		} else {
			$elapsed ['count'] = 1;
			$elapsed ['unit'] = 'month';
			return $elapsed;
		}
		
		if ($diff ['year'] > 0) {
			$elapsed ['count'] = $diff ['year'];
			$elapsed ['unit'] = 'year';
		}*/
		
		if ($elapsed ['count'] > 1) {
			$elapsed ['unit'] = $elapsed ['unit'] . 's';
		}
		return $elapsed;
	
	}
	
	public function getPrimaryImage() {
		$image = Model_Mapper_ItemResource::newInstance ()->getItemPrimary ( $this );
		
		if ($image) {
			return $image->getName ();
		} else {
			return '';
		}
	
	}
	
	public function getPrimaryImagePath() {
		$image = Model_Mapper_ItemResource::newInstance ()->getItemPrimary ( $this );
		
		if ($image) {
			return $image->getFilePath ();
		} else {
			return content_path () . 'default' . DS . 'default.jpg';
		}
	
	}
	
	public function getPrimaryImageURI() {
		$image = Model_Mapper_ItemResource::newInstance ()->getItemPrimary ( $this );
		if ($image) {
			return $image->getURI ();
		} else {
			return '';
		}
	}
	
	public function getLocationName() {
		$location = Model_Mapper_Locations::newInstance ()->getByID ( $this->getLocation_id () );
		if ($location) {
			return $location->getName ();
		} else {
			return '';
		}
	}
	
	public function getLocationURI() {
		$location = Model_Mapper_Locations::newInstance ()->getByID ( $this->getLocation_id () );
		if ($location) {
			return $location->getURI ();
		} else {
			return '';
		}
	}
	
	function getUser() {
		return Model_Mapper_User::newInstance ()->getByID ( $this->getUser_id () );
	}
	
	public function getTags() {
		return $this->tags;
	}
	public function setTags($value) {
		$this->tags = $value;
	}
	
	public function getViews() {
		return $this->views;
	}
	
	public function setViews($value) {
		$this->views = $value;
	}
	
	public function increaseView() {
		$this->views ++;
		$this->save ();
	}
	
	public function getType() {
		return $this->type;
	}
	
	public function setType($value) {
		$this->type = $value;
	}
	
	public function getURI() {
		return Model_Mapper_Items::newInstance ()->getURI ( $this );
	}
	
	public function getCategory() {
		return Model_Mapper_Categories::newInstance ()->getByID ( $this->getCategory_id () );
	}
	
	public function enable() {
		$this->setEnabled ( 1 );
		$this->save ();
	}
	
	public function disable() {
		$this->setEnabled ( 0 );
		$this->save ();
	}
	
	public function activate() {
		$this->setActive ( 1 );
		$this->save ();
		$this->setActivatedEmail ();
	}
	
	public function deActivate() {
		$this->setActive ( 1 );
		$this->save ();
	
	}
	
	public function getEditURL($admin = true) {
		return Model_Mapper_Items::newInstance ()->getEditURL ( $this, $admin );
	}
	
	public function getDeleteURL($admin = true) {
		return Model_Mapper_Items::newInstance ()->getDeleteURL ( $this, $admin );
	}
	
	public function __set($name, $value) {
		$method = 'set' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		$this->$method ( $value );
	}
	
	public function __get($name) {
		$method = 'get' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		return $this->$method ();
	}
	
	public function setOptions(array $options) {
		$methods = get_class_methods ( $this );
		foreach ( $options as $key => $value ) {
			$method = 'set' . ucfirst ( $key );
			if (in_array ( $method, $methods )) {
				$this->$method ( $value );
			}
		}
	}
	
	public function setItem_id($value) {
		$this->item_id = $value;
	}
	
	public function getItem_id() {
		return $this->item_id;
	}
	
	public function setYoutube_url($value) {
		$this->youtube_url = $value;
	}
	
	public function getYoutube_url() {
		return $this->youtube_url;
	}
	
	public function setName($value) {
		$this->name = $value;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function setDescription($value) {
		$this->description = $value;
	}
	
	public function getDescription() {
		return $this->description;
	}
	
	public function setUser_id($value) {
		$this->user_id = $value;
	}
	
	public function getUser_id() {
		return $this->user_id;
	}
	
	public function setCategory_id($value) {
		$this->category_id = $value;
	}
	
	public function getCategory_id() {
		return $this->category_id;
	}
	
	public function setPublish_date($value) {
		$this->publish_date = $value;
	}
	
	public function getPublish_date() {
		return $this->publish_date;
	}
	
	public function setModified_date($value) {
		$this->modified_date = $value;
	}
	
	public function getModified_date() {
		return $this->modified_date;
	}
	
	public function setExpires($value) {
		$this->expires = $value;
	}
	
	public function getExpires() {
		return $this->expires;
	}
	
	public function setPrice($value) {
		$this->price = $value;
	}
	
	public function getPrice() {
		return $this->price;
	}
	
	public function setCurrency_code($value) {
		$this->currency_code = $value;
	}
	
	public function getCurrency_code() {
		return $this->currency_code;
	}
	
	public function setContact_name($value) {
		$this->contact_name = $value;
	}
	
	public function getContact_name() {
		return $this->contact_name;
	}
	
	public function setContact_email($value) {
		$this->contact_email = $value;
	}
	
	public function getContact_email() {
		return $this->contact_email;
	}
	
	public function setPremium($value) {
		$this->premium = $value;
	}
	
	public function getPremium() {
		return $this->premium;
	}
	
	public function setEnabled($value) {
		$this->enabled = $value;
	}
	
	public function getEnabled() {
		return $this->enabled;
	}
	
	public function setActive($value) {
		$this->active = $value;
	}
	
	public function getActive() {
		return $this->active;
	}
	
	public function setSpam($value) {
		$this->spam = $value;
	}
	
	public function getSpam() {
		return $this->spam;
	}
	
	public function setSecret($value) {
		$this->secret = $value;
	}
	
	public function getSecret() {
		return $this->secret;
	}
	
	public function setShow_email($value) {
		$this->show_email = $value;
	}
	
	public function getShow_email() {
		return $this->show_email;
	}
	
	public function setSlug($value) {
		$this->slug = $value;
	}
	
	public function getSlug() {
		return $this->slug;
	}
	
	public function setUrgent($value) {
		$this->urgent = $value;
	}
	
	public function getUrgent() {
		return $this->urgent;
	}
	
	public function setAddress_line_1($value) {
		$this->address_line_1 = $value;
	}
	
	public function getAddress_line_1() {
		return $this->address_line_1;
	}
	
	public function setAddress_line_2($value) {
		$this->address_line_2 = $value;
	}
	
	public function getAddress_line_2() {
		return $this->address_line_2;
	}
	
	public function setLocal_area($value) {
		$this->local_area = $value;
	}
	
	public function getLocal_area() {
		return $this->local_area;
	}
	
	public function setLocation_id($value) {
		$this->location_id = $value;
	}
	
	public function getLocation_id() {
		return $this->location_id;
	}
	
	public function setPostcode($value) {
		$this->postcode = $value;
	}
	
	public function getPostcode() {
		return $this->postcode;
	}
	
	public function setUse_in_map($value) {
		$this->use_in_map = $value;
	}
	
	public function getUse_in_map() {
		return $this->use_in_map;
	}
	
	public function getTagsHtml() {
		$item = $this;
		$cat = Model_Mapper_Categories::newInstance ()->getByID ( $this->getCategory_id () );
		$c = $cat->getRoot ();
		$slug = $c->getSlug ();
		$result = array ();
		if (file_exists ( plugin_path () . $slug . DS . 'tags.php' )) {
			include plugin_path () . $slug . DS . 'tags.php';
		}
		return $result;
	}
	
	public function getActivationURI() {
		if ($this->getSecret () == '') {
			$this->setSecret ( sha1 ( createRandomPassword ( 40 ) ) );
			$this->save ();
		}
		return site_url () . 'item/activate/' . $this->getSecret ();
	}
	
	public function setActivatedEmail() {
		$mailer = new Email_Manager ();
		$mailer->IsHTML ();
		$mailer->Subject = 'Your Ad activated at myhighst.com';
		$mailer->AddReplyTo ( 'info@myhighst.com', 'Myhighst.com' );
		$mailer->SetFrom ( 'info@myhighst.com', 'Myhighst.com' );
		$user = $this->getUser ();
		$mailer->AddAddress ( $user->getEmail (), $user->getFirst_name () );
		$msg = file_get_contents ( template_path () . 'email' . DS . 'item-activated.phtml' );
		$msg = str_replace ( '{first_name}', $user->first_name, $msg );
		$msg = str_replace ( '{link}', $this->getURI (), $msg );
		//$msg = str_replace ( '{link_text}', 'Click here to activate', $msg );
		$mailer->MsgHTML ( $msg );
		$mailer->Send ();
	}
	
	public function sendActivationEmail() {
		$mailer = new Email_Manager ();
		$mailer->IsHTML ();
		$mailer->Subject = 'Activate Your Ad-Myhighst.com';
		$mailer->AddReplyTo ( 'info@myhighst.com', 'Myhighst.com' );
		$mailer->SetFrom ( 'info@myhighst.com', 'Myhighst.com' );
		$user = $this->getUser ();
		$mailer->AddAddress ( $user->getEmail (), $user->getFirst_name () );
		$msg = file_get_contents ( template_path () . 'email' . DS . 'activate.phtml' );
		$msg = str_replace ( '{first_name}', $user->first_name, $msg );
		$msg = str_replace ( '{link}', $this->getActivationURI (), $msg );
		$msg = str_replace ( '{link_text}', 'Click here to activate', $msg );
		$mailer->MsgHTML ( $msg );
		$mailer->Send ();
	}
	
	public function save() {
		if ($this->name == '') {
			throw new Exception ( 'Item Name is not specified.Aborted' );
		}
		if ($this->slug == '') {
			$this->slug = sanitizeSlug ( $this->name );
		}
		$count = 1;
		if ($this->getItem_id () == null) {
			while ( checkSlug ( $this->slug ) ) {
				$parts = explode ( '_', $this->slug );
				$count = ( int ) $parts [count ( $parts ) - 1] + 1;
				$this->slug = $this->slug . '_' . $count;
			}
		}
		
		$date = date ( 'Y-m-d H:i:s' );
		if ($this->publish_date == '') {
			$this->setPublish_date ( $date );
		}
		if ($this->modified_date == '') {
			$this->setModified_date ( $date );
		}
		Model_Mapper_Items::newInstance ()->save ( $this );
	}
	
	public function toArray() {
		return get_object_vars ( $this );
	}

}